package org.moore.view;

import org.moore.controller.BlockSwapListener;
import org.moore.model.Block;
import org.moore.model.Map;
import org.moore.utils.Countdown;
import org.moore.utils.Data;

import javax.swing.*;
import java.awt.*;

import static org.moore.utils.Button.getjButton;

// 游戏界面游戏面板组件类
public class GameLabel extends JLabel {
    // 定义提示按钮
	private final JButton tip;
    // 定义暂停按钮
    private final JButton pause;
    // 定义游戏模式文本
    private String gameMode;
    // 定义游戏关卡文本
    private String gameLevel;

    public JButton getTip() {
        return tip;
    }

    public JButton getPause() {
        return pause;
    }

    public GameLabel() {
        // 创建提示按钮
        tip = getjButton(Data.TIP_IMAGE, null);
        tip.setBounds(1020,500,75,75);
        this.add(tip);
        // 添加地图鼠标监听
        BlockSwapListener blockSwapListener = new BlockSwapListener();
        this.addMouseListener(blockSwapListener);
        this.addMouseMotionListener(blockSwapListener);
        // 将首次点击清空
        Data.COUNT = 0;
        // 使界面获得焦点
        this.requestFocusInWindow();
        // 绘制暂停按钮
        pause = getjButton(Data.PAUSE_IMAGE,null);
        pause.setBounds(1110,505,75,75);
        this.add(pause);
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 使用g2d绘制以修复抗锯齿
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 关卡展示
        if (Map.isMapType() == 1){
            gameMode = "步数";
        } else if (Map.isMapType() == 2){
            gameMode = "时间";
        }
        if (Map.getLevel() == 1){
            gameLevel = "初级";
        } else if (Map.getLevel() == 2){
            gameLevel = "中级";
        } else if (Map.getLevel() == 3){
            gameLevel = "高级";
        }
        // 绘制背景图片
        g2d.drawImage(Data.GAME_BACKGROUND,0,0,this.getWidth(),this.getHeight(),null);
        // 设置字体
        g2d.setFont(Data.FONT);
        // 设置颜色
        g2d.setColor(Color.WHITE);
        // 绘制文字背景
        g2d.drawImage(Data.TEXT_IMAGE,950,20,250,180,null);
        // 绘制关卡文字
        g2d.drawString(gameMode + gameLevel,1040,90);
        // 绘制分数
        g2d.drawString("分数" + Data.SCORE,1025,120);
        // 绘制文字背景
        g2d.drawImage(Data.TEXT_IMAGE,950,190,250,180,null);
        // 绘制时间剩余文字
        g2d.drawString("剩余时间",1035,260);
        // 绘制时间
        g2d.drawString("00" + Countdown.getSeconds() + "秒",1050,290);
        // 绘制文字背景
        g2d.drawImage(Data.TEXT_IMAGE,950,340,250,180,null);
        // 绘制步数剩余文字
        g2d.drawString("剩余步数",1035,410);
        // 绘制步数
        g2d.drawString("00" + Data.STEPS + "步",1050,440);
        // 绘制方格用于地图框架，并绘制消除物
        g2d.setColor(Color.GRAY);
        // 获取数组
        Block[][] blocks = Map.getMap();
        for (int i = 0; i < blocks.length; i++) {
            for (int j = 0; j < blocks[0].length; j++) {
                g2d.drawRect(Data.START_X + j * Data.GRID,Data.START_Y + i * Data.GRID, Data.W, Data.H);
                if (blocks[i][j] != null) {
                    blocks[i][j].showMe(g2d);
                    repaint();
                }
            }
        }
    }
}
